package com.alibaba.citrus.ots.unified.inventory.facade.service;

import com.alibaba.citrus.ots.unified.inventory.repository.AllocateRelationReadRepository;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsunifiedinventory.api.allocaterelationread.dto.QueryAllocateRelationForOrderRequest;
import com.epoch.app.otsunifiedinventory.api.allocaterelationread.service.AllocateRelationReadService;
import com.epoch.app.otsunifiedinventory.dto.request.QueryAllocateRelationRequest;
import com.epoch.app.otsunifiedinventory.sdo.AllocateRelationSDO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author wangcheng
 * @date 2021/10/12
 */
@Service
@Primary
public class AllocateRelationReadServiceImpl implements AllocateRelationReadService {

    @Resource
    AllocateRelationReadRepository allocateRelationReadRepository;
    @Override
    public Result<List<AllocateRelationSDO>> pageQueryAllocateRelation(
        QueryAllocateRelationRequest queryAllocateRelationRequest) {
        AllocateRelationSDO allocateRelationSDO = queryAllocateRelationRequest.getAllocateRelationSDO();
        PageResult<List<AllocateRelationSDO>> allocateRelationaList = allocateRelationReadRepository.queryPageList(allocateRelationSDO);
        if (allocateRelationaList!=null && CollectionUtils.isNotEmpty(allocateRelationaList.getContent())) {
            return Result.listSuccess(allocateRelationaList.getContent(), allocateRelationaList.getContent().size());
        }
        return Result.listEmptySuccess();
    }
    @Override
    public Result<AllocateRelationSDO> pageQueryAllocateRelationDetails(
            QueryAllocateRelationRequest queryAllocateRelationRequest) {
        AllocateRelationSDO allocateRelationSDO = queryAllocateRelationRequest.getAllocateRelationSDO();
        PageResult<List<AllocateRelationSDO>> allocateRelationaList = allocateRelationReadRepository.queryPageList(allocateRelationSDO);
        List<AllocateRelationSDO> content = allocateRelationaList.getContent();
        if(CollectionUtils.isEmpty(content)){
            return null;
        }
        return Result.success(content.get(0));
    }

    @Override
    public Result<List<AllocateRelationSDO>> queryAllocateRelationForOrder(QueryAllocateRelationForOrderRequest queryAllocateRelationForOrderRequest) {
        PageResult<List<AllocateRelationSDO>> result = null;
        AllocateRelationSDO allocateRelationSDO = new AllocateRelationSDO();
        allocateRelationSDO.setIsEnable(true);
        allocateRelationSDO.setStart(0);
        allocateRelationSDO.setLimit(1);
        //优先根据销售组织+客户查询收发货仓
        allocateRelationSDO.setAddressCode(queryAllocateRelationForOrderRequest.getAddressCode());
        allocateRelationSDO.setOrganizationCode(queryAllocateRelationForOrderRequest.getOrganizationCode());
        result = allocateRelationReadRepository.queryPageList(allocateRelationSDO);
        if (result!=null && CollectionUtils.isNotEmpty(result.getContent())){
            return Result.listSuccess(result.getContent(), result.getContent().size());
        }
        //若无法找到根据销售组织+销售渠道+销售部门查找发货仓
        allocateRelationSDO.setAddressCode("");
        allocateRelationSDO.setSaleChannelCode(queryAllocateRelationForOrderRequest.getSaleChannelCode());
        allocateRelationSDO.setDepartmentCode(queryAllocateRelationForOrderRequest.getSaleDepartmentCode());
        result = allocateRelationReadRepository.queryPageList(allocateRelationSDO);
        if (result!=null && CollectionUtils.isNotEmpty(result.getContent())){
            return Result.listSuccess(result.getContent(), result.getContent().size());
        }
        return Result.listEmptySuccess();
    }
}
